package nio;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * Description: channel demo
 * Created by 4790K_NSL on 2017/5/14.
 */
public class ChannelDemo {

    public static void main(String[] args) throws IOException {
        RandomAccessFile file=new RandomAccessFile("d:/java_test/demo.txt","rw");
        FileChannel inChannel=file.getChannel();
        ByteBuffer buf=ByteBuffer.allocate(30);
        int byteRead=inChannel.read(buf);
        System.out.println(inChannel.size());
        while (byteRead!=-1){
            System.out.println(("Read : " + byteRead));
            System.out.println(inChannel.position());
            buf.flip();

           /* while (buf.hasRemaining()){
                System.out.println(buf.get());
            }*/
            String str=new String(buf.array(),"utf8");
            System.out.println(str);
            buf.clear();
            byteRead=inChannel.read(buf);

        }

        inChannel.close();
        file.close();

    }
}
